//package cn.changeforyou.analyzer.netDemo;
//
//import cn.changeforyou.utils.io.read.ReadUtils;
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.util.HashSet;
//import java.util.Set;
//import org.eclipse.core.resources.IProject;
//import org.eclipse.core.resources.ResourcesPlugin;
//import org.eclipse.core.runtime.CoreException;
//import org.eclipse.jdt.core.ICompilationUnit;
//import org.eclipse.jdt.core.IJavaProject;
//import org.eclipse.jdt.core.IPackageFragment;
//import org.eclipse.jdt.core.JavaCore;
//import org.eclipse.jdt.core.dom.AST;
//import org.eclipse.jdt.core.dom.ASTNode;
//import org.eclipse.jdt.core.dom.ASTParser;
//import org.eclipse.jdt.core.dom.ASTVisitor;
//import org.eclipse.jdt.core.dom.Block;
//import org.eclipse.jdt.core.dom.CompilationUnit;
//import org.eclipse.jdt.core.dom.FieldDeclaration;
//import org.eclipse.jdt.core.dom.Javadoc;
//import org.eclipse.jdt.core.dom.MethodDeclaration;
//import org.eclipse.jdt.core.dom.SimpleName;
//import org.eclipse.jdt.core.dom.Statement;
//import org.eclipse.jdt.core.dom.TypeDeclaration;
//import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
//import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
//import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
//import org.eclipse.jface.text.BadLocationException;
//import org.eclipse.jface.text.Document;
//import org.eclipse.text.edits.MalformedTreeException;
//import org.eclipse.text.edits.TextEdit;
//import org.junit.jupiter.api.Test;
//
///**
// * @author zhyu
// * @date 2021/1/30 13:24
// */
//
//public class ASTRewriteDemo {
//
//    public static void parse(String str) {
//        ASTParser parser = ASTParser.newParser(AST.JLS8);
//        parser.setSource(str.toCharArray());
//        parser.setKind(ASTParser.K_COMPILATION_UNIT);
//        org.eclipse.jface.text.Document document = new org.eclipse.jface.text.Document(str);
//        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
//        CompilationUnit unit = (CompilationUnit) parser.createAST(null);
//        TypeDeclaration type = (TypeDeclaration) unit.types().get(0);
//        cu.accept(new ASTVisitor() {
//            Set names = new HashSet();
//
//            public boolean visit(VariableDeclarationFragment node) {
//                System.out.println(node);
//                SimpleName name = node.getName();
//                this.names.add(name.getIdentifier());
//                System.out.println("Declaration of '" + name + "' at line"
//                    + cu.getLineNumber(name.getStartPosition()));
//                return false; // do not continue
//            }
//
//            public boolean visit(SimpleName node) {
//                System.out.println(node);
//                if (this.names.contains(node.getIdentifier())) {
//                    System.out.println("Usage of '" + node + "' at line "
//                        + cu.getLineNumber(node.getStartPosition()));
//                }
//                return true;
//            }
//        });
//        cu.recordModifications();
//        ASTRewrite rewriter = ASTRewrite.create(cu.getAST());
//        FieldDeclaration field = type.getFields()[0];
//        ListRewrite listRewrite = rewriter.getListRewrite(field, FieldDeclaration.FRAGMENTS_PROPERTY);
//
//
//        TextEdit edits = rewriter.rewriteAST(document, null);
//        try {
//            edits.apply(document);
//        } catch (BadLocationException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    //read file content into a string
//    public static String readFileToString(String filePath) throws IOException {
//        StringBuilder fileData = new StringBuilder(1000);
//        BufferedReader reader = new BufferedReader(new FileReader(filePath));
//
//        char[] buf = new char[10];
//        int numRead = 0;
//        while ((numRead = reader.read(buf)) != -1) {
//            System.out.println(numRead);
//            String readData = String.valueOf(buf, 0, numRead);
//            fileData.append(readData);
//            buf = new char[1024];
//        }
//
//        reader.close();
//
//        return fileData.toString();
//    }
//
//    //loop directory to get file list
//    public static void ParseFilesInDir() throws IOException {
//        File root = new File("D:\\code\\analyzer\\analyzer\\src\\test\\resources\\javaFiles\\HelloWorld.java");
//        if (root.isFile()) {
//            parse(readFileToString(root.getAbsolutePath()));
//        }
//    }
//
//    public static void main(String[] args) throws IOException {
//        ParseFilesInDir();
//    }
//}
